package methodObj;

public class Demo3Binary {
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6,7,8,9};
        int index=binary(arr,5);
        System.out.println(index);
    }
    //二分查找算法
    public static int binary(int[] arr,int data){
        //定义三个变量，分别代表最大索引，最小索引，中间索引
        int min=0;
        int max=arr.length-1;
        int mid=0;
        //查找
        while(min<=max){
            mid=(min+max)/2;
            if (data > arr[mid]) {
                min=mid+1;
            }else if(data<arr[mid]){
                max=mid-1;
            }else{
                return mid;
            }
        }
        return -1;
    }
}
